package com.example.demo.controller;

import com.example.demo.entity.Buyer.BuyPlanBill;
import com.example.demo.entity.Buyer.BuyPlanBillDetail;
import com.example.demo.services.BuyPlanBillService;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class BuyPlanBillController {
    private final BuyPlanBillService buyPlanBillService;

    public BuyPlanBillController(BuyPlanBillService buyPlanBillService) {
        this.buyPlanBillService = buyPlanBillService;
    }

    @GetMapping("/buyPlan")
    public List<BuyPlanBill> getAll() {
        return buyPlanBillService.getAllBuyPlanBills();
    }

    @GetMapping("/getByBillNo")
    public List<BuyPlanBill> getByBillNo(String billNo) {
        //System.out.println("1:" + billNo);
        return buyPlanBillService.getBuyPlanBill(billNo);
    }

    @PostMapping("/getDetail")
    public List<BuyPlanBillDetail> getDetail(@RequestBody Map<String, String> payload) {
        String billNo = payload.get("billNo");
        System.out.println(buyPlanBillService.getDetail(billNo));
        return buyPlanBillService.getDetail(billNo);
    }

    @PostMapping("/getBuyer")
    public List<Object> getBuyer() {
        return buyPlanBillService.getBuyer();
    }

    @PostMapping("/setForm")
    public int setForm(@RequestBody Map<String, String> payload) throws ParseException {
        String billNo = payload.get("billNo");
        String planDateStr = payload.get("planDate");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        Date planDate = sdf.parse(planDateStr);
        String buyerCode = payload.get("buyerCode");
        String remark = payload.get("remark");
        return buyPlanBillService.setForm(billNo, planDate, buyerCode, remark);
    }
}
